﻿#region Libraries

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.DataVisualization.Charting;

#endregion

namespace XYZ_Inventory
{
    public partial class SuppliersReports : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Chart1.ChartAreas[0].AxisY.Title = "Raw material Quantity";
                Chart1.ChartAreas[0].AxisX.Title = "Supplier";
                txtFromDate.Text = DateTime.Now.ToShortDateString();
                Calendar1.SelectedDate = DateTime.Now.Date;
                txtToDate.Text = DateTime.Now.ToShortDateString();
                Calendar2.SelectedDate = DateTime.Now.Date;
                LoadData();
            }
        }

        private void LoadData()
        {
            Chart1.Series["Series"].ChartType =
                (SeriesChartType) Enum.Parse(typeof (SeriesChartType), "17");

            Chart2.Series["Series"].ChartType =
                (SeriesChartType) Enum.Parse(typeof (SeriesChartType), "17");

            Series series1 = Chart1.Series["Series"];
            Series series2 = Chart2.Series["Series"];

            XYZDBContext dbContext = new XYZDBContext();

            DateTime fromDateTime = Calendar1.SelectedDate;
            DateTime toDateTime = Calendar2.SelectedDate;
            List<SupplierResults> results1 =
                dbContext.RawMaterialOrders.Where(x => x.DeliveryDate <= toDateTime && x.DeliveryDate >= fromDateTime)
                    .GroupBy(x => x.SupplierID)
                    .Select(
                        y =>
                            new SupplierResults
                            {
                                SupplierId = y.FirstOrDefault().SupplierID,
                                Quantity = y.Sum(z => z.Quantity)
                            }).ToList();
            foreach (SupplierResults r in results1)
            {
                string supplierName = dbContext.Suppliers1.Single(x => x.ID == r.SupplierId).Name;
                series1.Points.AddXY(r.SupplierId + " - " + supplierName, r.Quantity);
            }

            List<SupplierResults> results2 =
                dbContext.RawMaterialOrders.Where(x => x.DeliveryDate <= toDateTime && x.DeliveryDate >= fromDateTime)
                    .Join(dbContext.RawMaterialInventories,
                        x => x.RawMaterialID, y => y.RawMaterialID, (a, b) => new SupplierResults
                        {
                            SupplierId = a.SupplierID,
                            Amount = a.Quantity*b.UnitBuyingPrice
                        })
                    .GroupBy(z => z.SupplierId)
                    .Select(
                        a =>
                            new SupplierResults
                            {
                                SupplierId = a.FirstOrDefault().SupplierId,
                                Amount = a.Sum(b => b.Amount)
                            })
                    .ToList();

            foreach (SupplierResults r in results2)
            {
                string supplierName = dbContext.Suppliers1.Single(x => x.ID == r.SupplierId).Name;
                series2.Points.AddXY(r.SupplierId + " - " + supplierName, r.Amount);
            }
        }

        protected void btnFromDate_OnClick(object sender, EventArgs e)
        {
            Calendar1.Visible = !Calendar1.Visible;
        }

        protected void btnToDate_OnClick(object sender, EventArgs e)
        {
            Calendar2.Visible = !Calendar2.Visible;
        }

        protected void Calendar2_OnSelectionChanged(object sender, EventArgs e)
        {
            txtToDate.Text = Calendar2.SelectedDate.ToShortDateString();
            Calendar2.Visible = false;
            LoadData();
        }

        protected void Calendar1_OnSelectionChanged(object sender, EventArgs e)
        {
            txtFromDate.Text = Calendar1.SelectedDate.ToShortDateString();
            Calendar1.Visible = false;
            LoadData();
        }
    }

    public class SupplierResults
    {
        public int SupplierId { get; set; }
        public int Quantity { get; set; }
        public float Amount { get; set; }
    }
}